'''
爬虫类
'''
import requests
from bs4 import BeautifulSoup
class Scrawler():
    def __init__(self):
        self.headers={
            "User-Agent":"Mozilla / 5.0(Windows NT 10.0;Win64;x64) "
                         "AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 76.0.3809.132  Safari / 537.36"
        }

    def getCodes(self,url):
        rs = requests.get(url,headers=self.headers)
        rs.encoding = rs.apparent_encoding
        return rs.text

    def getTargetTexts(self,url,selector):
        bs = BeautifulSoup(self.getCodes(url),"html.parser")
        return [item.text for item in bs.select(selector=selector)]

    def getTargethrefs(self,url,selector):
        bs = BeautifulSoup(self.getCodes(url),"html.parser")
        return [item.attrs['href'] for item in bs.select(selector=selector)]

    def getTargetImageUrl(self,url,selector):
        bs = BeautifulSoup(self.getCodes(url), "html.parser")
        return [item.attrs['src'] for item in bs.select(selector=selector)]

    def getTargetImage(self,imgurl,index):
        b2codes = requests.get(imgurl,headers=self.headers).content
        with open("target"+str(index)+".png",'wb') as f:
            f.write(b2codes)